(function() {
	function Request() {
		var isout = true;
		var domains = ['127.0.0.1', 'localhost', '0.0.0.0', '192.168.'];
		for (var i = 0; i < domains.length; i++) {
			if (window.location.href.indexOf(domains[i]) > -1) {
				isout = false;
				break;
			}
		}
		this.baseUrl = isout ? 'http://api.crqcn.com:3001' : 'http://localhost:3001';
	}
	Request.prototype = {
		get: function(url, data, cb, errcb) {
			var xmlhttp = new XMLHttpRequest();
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState === 4) {
					if (xmlhttp.status === 200) {
						if (xmlhttp.response.flag === 'True') {
							if (cb instanceof Function) {
								cb(xmlhttp.response);
							}
						} else {
							if (errcb instanceof Function) {
								errcb(xmlhttp.response);
							} else {
								alert(xmlhttp.response.message);
							}
						}
					} else {
						alert('网络异常，请联系管理员');
					}
				}
			};
			xmlhttp.responseType = 'json';
			xmlhttp.open('GET', this.baseUrl + url, true);
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.send(this.format(data));
		},
		post: function(url, data, cb, errcb) {
			var xmlhttp = new XMLHttpRequest();
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState === 4) {
					if (xmlhttp.status === 200) {
						if (xmlhttp.response.flag === 'True') {
							if (cb instanceof Function) {
								cb(xmlhttp.response);
							}
						} else {
							if (errcb instanceof Function) {
								errcb(xmlhttp.response);
							} else {
								alert(xmlhttp.response.message);
							}
						}
					} else {
						alert('服务异常，请联系管理员');
					}
				}
			};
			xmlhttp.responseType = 'json';
			xmlhttp.open('POST', this.baseUrl + url, true);
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.send(this.format(data));
		},
		format: function(data) {
			var params = [];
			for (var key in data) {
				params.push(key + '=' + data[key]);
			}
			return params.join('&');
		},
	};
	window.Request = new Request();
})();
